﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinLife.Application.Interface;
using XinLife.Core.Extention;
using XinLife.Core.Helper;
using XinLife.Core.Model;
using XinLife.Model.Busines;

namespace XinLife.Application.Implement
{
    public class BlacklistService : BaseService, IBlacklistService
    {
        public BlacklistService(IBaseRepository baseRepository) : base(baseRepository)
        {
        }

        public async Task<PageData<object>> GetBlacklistPageList(Pagination page, string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            string? cName = queryParam.Value<string>("name");
            string? ip = queryParam.Value<string>("ip");

            RefAsync<int> totalCount = 0;
            var data = await _db.Queryable<Blacklist>()
                                .Includes(a=>a.user)
                                .WhereIF(!cName.IsEmpty(), a => a.cUserId.Contains(cName))
                                .WhereIF(!ip.IsEmpty(), a => a.cIp == ip)
                                .ToPageListAsync(page.pageIndex, page.pageSize, totalCount);

            return new PageData<object>()
            {
                count = totalCount,
                entitys = data
            };
        }
        public async Task<AjaxResult> GetBlacklistForm(string id)
        {
            var data = await _db.Queryable<Blacklist>().FirstAsync(t => t.cId == id);
            return Success(data);
        }


        public async Task<AjaxResult> EditBlacklist(Blacklist blacklist)
        {
            var isExt = await _db.Queryable<Blacklist>()
                .WhereIF(blacklist.cIp.IsNotEmpty(), t => t.cIp == blacklist.cIp && t.cState == true)
                .WhereIF(blacklist.cUserId.IsNotEmpty(), t => t.cUserId == blacklist.cUserId && t.cState == true)
                .WhereIF(!blacklist.cId.IsEmpty(), t => t.cId != blacklist.cId)
                .AnyAsync();
            if (isExt)
            {
                return Error("该证件已在有黑名单限期内");
            }

            if (string.IsNullOrWhiteSpace(blacklist.cId))
            {
                blacklist.Create(AspNetUser.User.cId);
                await _db.Insertable(blacklist).ExecuteCommandAsync();
                return Success("添加成功");
            }
            else
            {
                var old = await _db.Queryable<Blacklist>().FirstAsync(t => t.cId == blacklist.cId);
                blacklist.cCreatedBy = old.cCreatedBy;
                blacklist.cCreatedDate = old.cCreatedDate;

                await _db.Updateable<Blacklist>(blacklist).ExecuteCommandAsync();
                return Success("修改成功");
            }
        }
        public async Task<AjaxResult> DelBlacklist(string ids)
        {
            var r = await _db.Deleteable<Blacklist>().In(ids.Split(',')).ExecuteCommandAsync();
            if (r > 0)
            {
                return Success("删除成功");
            }
            else
            {
                return Success("删除失败");
            }
        }
        public async Task<AjaxResult> SetInvalid(string cId)
        {
            await _db.Updateable<Blacklist>()
                     .SetColumns(t => t.cState, false)
                     .SetColumns(t => t.cUpdatedBy, AspNetUser.User.cId)
                     .SetColumns(t => t.cUpdatedDate, DateTime.Now)
                     .Where(t => t.cId == cId)
                     .ExecuteCommandAsync();
            return Success("ok");
        }
        public Task HandleBlacklistData()
        {
            return _db.Updateable<Blacklist>()
                      .SetColumns(t => t.cState, false)
                      .Where(t => t.cForbiddenEndTime.Date < DateTime.Now.Date)
                      .ExecuteCommandAsync();
        }
    }
}
